package cn.edu.zut.hkxy.controller;

import cn.edu.zut.hkxy.pojo.Admin;
import cn.edu.zut.hkxy.pojo.AdminExample;
import cn.edu.zut.hkxy.pojo.AjaxResult;
import cn.edu.zut.hkxy.service.UsersService;
import cn.edu.zut.hkxy.util.DateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UsersService usersService;

    @RequestMapping("/admin-list")
    public String toAdminList(){
        return "adminList";
    }
    @RequestMapping("/admin-list-json")
    @ResponseBody
    public List<Admin> getAdminList(@RequestParam(required = false) String key,@RequestParam Integer pageNum){
        AdminExample example = new AdminExample();
        example.or().andUsernameLike("%" + key + "%").andPowerNotLike("%all%");
        example.or().andRealnameLike("%" + key + "%").andPowerNotLike("%all%");
        List<Admin> list=usersService.getAdminPage(example,pageNum);
        return list;
    }
    @RequestMapping("/admin-add")
    @ResponseBody
    public AjaxResult addAdmin(Admin admin){
        System.out.println(Arrays.toString(admin.getPowers()));
        String[] powers=admin.getPowers();
        if(powers!=null) {
            String power = Arrays.toString(powers).replace("[", "").replace("]", "").replace(" ", "");
            admin.setPower(power);
        }
        if(admin.getPassword()==null||admin.getPassword().equals("")){
            admin.setPassword("123456");
        }
        int i=usersService.addAdmin(admin);
        AjaxResult result=new AjaxResult();
        if(i==1){
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }else{
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
        }
        return result;
    }
    @RequestMapping("/admin-delete")
    @ResponseBody
    public String deleteAdmin(@RequestParam Integer id){
        usersService.deleteById(id);
        return "success";
    }
    @RequestMapping("/admin-modify")
    @ResponseBody
    public AjaxResult modifyAdmin(@RequestParam Integer id,@RequestParam(required = false)String power){
        System.out.println(power);
        Admin admin=usersService.getAdminById(id);
        admin.setPower(power);
       Boolean b=usersService.modifyAdmin(admin);
        AjaxResult result=new AjaxResult();
        if(b){
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }else{
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
        }
        return result;
    }

    @RequestMapping(path = "/admin/{id}")
    @ResponseBody
    public Admin getAdminById(@PathVariable int id){
        Admin admin=usersService.getAdminById(id);
        return admin;
    }

    @RequestMapping("/personal")
    public String toPersonal(){
        return "Personal";
    }

    @RequestMapping("/personal-update")
    @ResponseBody
    public AjaxResult updatePersonal(Admin admin,
                                     @RequestParam(required = false) String birth,
                                     @RequestParam(required = false) String newpassword,
                                     @RequestParam(required = false) String oldpassword,
                                     HttpSession session){
   //     System.out.println(admin);
   //     System.out.println(birth);
        AjaxResult result=new AjaxResult();
        try {
            if(birth==null||birth.equals("")){

            }else{
                Date birthday=DateFormat.formatToDateOnlyDate(birth);
                admin.setBirthday(birthday);
            }
            usersService.updateAdmin(admin);
            session.setAttribute("admin",usersService.getAdminById(admin.getId()));
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }

    @RequestMapping("/personal-change-password")
    @ResponseBody
    public AjaxResult updatePersonal(@RequestParam Integer id,
                                     @RequestParam String newpassword,
                                     @RequestParam String oldpassword){
        AjaxResult result=new AjaxResult();
        try {
            Admin admin=new Admin();
            admin.setId(id);
            String password=usersService.getAdminById(id).getPassword();
            if(oldpassword.equals(password)){
               admin.setPassword(newpassword);
               usersService.updateAdmin(admin);
               result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
            }else{
                result.setCode(AjaxResult.RESULT_MESSAGE_CODE);
                result.setMessage("原密码不正确！");
            }
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
}
